
#include <BRender>
#include <BPainter>
#include <BScene>
#include <BShape>
#include <BViewer>
#include <BPicker>
#include <BMouseEvent>
#include <BKeyEvent>

#include "TargetHandler.h"

TargetHandler::TargetHandler()
{
	_transStep = 0;
}
TargetHandler::~TargetHandler()
{

}

void TargetHandler::render(BRender& render)
{
	render.setLighting(false);
	render.setLineSmooth(true);
	render.setLineWidth(3);
	if (_targetNode)
	{
		render.setColor(255, 255, 255);
		render.drawSpace(_targetNode->space());
		if (_targetShape)
		{
			render.setColor(100, 255, 100);
			render.drawSpace(_targetShape->space());
		}
	}
	else
	{
		if (_targetShape)
		{
			render.setColor(100, 255, 100);
			render.drawSpace(_targetShape->space());
		}
	}

	render.setLineWidth(1);
	if (_hoveredNode)
	{
		render.setColor(255, 255, 255);
		render.drawSpace(_hoveredNode->space());
		if (_hoveredShape)
		{
			render.setColor(100, 255, 100);
			render.drawSpace(_hoveredShape->space());
		}
	}
	else
	{
		if (_hoveredShape)
		{
			render.setColor(100, 255, 100);
			render.drawSpace(_hoveredShape->space());
		}
	}

	//BHolder<BMap<BShape*, BBox>> shapeQueue = new BMap<BShape*, BBox>();
	//if (this->camera()->query("shapes", shapeQueue))
	//{
	//	render.setLineWidth(1);
	//	render.setCullback(false);
	//	render.setColor(255, 255, 255);
	//	for (auto it = shapeQueue->begin(); it.valid(); it++)
	//	{
	//		BShape* shape = it.key();
	//		render.drawSpace(shape->space());
	//	}
	//}

}

void TargetHandler::updateEvent(const BEvent& event)
{
	if (_transStep > 0)
	{
		BVector center = this->camera()->center();
		BVector destination = center;
		if (_targetShape)
			destination = _targetShape->space().center();
		if (_targetNode)
			destination = _targetNode->space().center();
		this->camera()->setCenter(center + (destination - center) / _transStep);
		_transStep--;
		if (_transStep == 0)
		{
			this->camera()->setCenter(destination);
		}
	}
}

bool TargetHandler::keyDownEvent(const BKeyEvent& keyEvent)
{
	Key key = keyEvent.key();
	BCamera* camera = this->camera();
	if (camera && key == Key_Space)
	{
		if(_targetNode)
			this->focus(_targetNode->space());
		else if(_targetShape)
			this->focus(_targetShape->space());
		return true;
	}

	return false;
}

bool TargetHandler::mouseMoveEvent(const BMouseEvent& mouseEvent)
{
	if (_transStep > 0)
		return true;
	if (mouseEvent.button() == Button_None)
	{
		const BPoint& pos = mouseEvent.pos();
		BLine line = this->viewer()->line(pos);
		emit("line-changed", line);
		BPicker picker(line);
		if (const BPickItem* item = picker.apply(this->scene()))
		{
			if (_hoveredNode != item->node() || _hoveredShape != item->shape())
			{
				_hoveredNode = item->node();
				_hoveredShape = item->shape();
				this->fresh();
			}
		}
	}
	return BSphereHandler::mouseMoveEvent(mouseEvent);
}

bool TargetHandler::mouseClickEvent(const BMouseEvent& mouseEvent)
{
	if (mouseEvent.button() == Button_Left)
	{
		const BPoint& pos = mouseEvent.pos();
		BLine line = this->viewer()->line(pos);

		BPicker picker(line);
		if (const BPickItem* item = picker.apply(this->scene()))
		{
			_targetNode = item->node();
			_targetShape = item->shape();
			_transStep = 10;
			this->fresh();
		}
	}
	return true;
}
